package com.cy.ppj.service.productannouncement.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cy.ppj.dao.mapper.productannouncement.ProductAnnouncementMapper;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.domian.productannouncement.ProductAnnouncement;
import com.cy.ppj.model.enums.CommonStatus;
import com.cy.ppj.model.query.productannouncement.ProductAnnouncementAdminQuery;
import com.cy.ppj.model.query.productannouncement.ProductAnnouncementQuery;
import com.cy.ppj.model.vo.admin.productannouncement.ProductAnnouncementAdminVO;
import com.cy.ppj.model.vo.front.productannouncement.ProductAnnouncementVO;
import com.cy.ppj.service.BaseService;
import com.cy.ppj.service.productannouncement.ProductAnnouncementService;

/**
 * 产品与公告service
 *
 * @author: tanyilin
 * @create: 2018年10月15日 下午5:19:40
 */
@Service
public class ProductAnnouncementServiceImpl extends BaseService implements ProductAnnouncementService{

	@Autowired
	private ProductAnnouncementMapper productAnnouncementMapper;
	
	@Override
	@Transactional
	public void addProductAnnouncement(ProductAnnouncement productAnnouncement) {
		Date time = new Date();
		productAnnouncement.setStatus(CommonStatus.ENABLE);
		productAnnouncement.setCreateTime(time);
		productAnnouncement.setUpdateTime(time);
		productAnnouncementMapper.insertSelective(productAnnouncement);
	}

	@Override
	@Transactional
	public void updateProductAnnouncement(ProductAnnouncement productAnnouncement) {
		Date time = new Date();
		productAnnouncement.setUpdateTime(time);
		productAnnouncementMapper.updateByPrimaryKeySelective(productAnnouncement);
	}

	@Override
	public PageBean<ProductAnnouncementAdminVO> selectProductAnnouncementAdmin(ProductAnnouncementAdminQuery query) {
		int total = productAnnouncementMapper.selectProductAnnouncementAdminCount(query);
		List<ProductAnnouncementAdminVO> list = new ArrayList<>();
		if(total > 0) {
			query.startPage();
			list = productAnnouncementMapper.selectProductAnnouncementAdminList(query);
			list.stream().forEach(p -> {
				p.setTypeText(p.getType().getText());
			});
		}
		return new PageBean<>(list, total);
	}

	@Override
	@Transactional
	public void updateStatus(ProductAnnouncement productAnnouncement) {
		productAnnouncementMapper.updateByPrimaryKeySelective(productAnnouncement);
	}

	@Override
	public PageBean<ProductAnnouncementVO> selectProductAnnouncement(ProductAnnouncementQuery query) {
		int total = productAnnouncementMapper.selectProductAnnouncementCount(query);
		List<ProductAnnouncementVO> list = new ArrayList<>();
		if(total > 0) {
			query.startPage();
			list = productAnnouncementMapper.selectProductAnnouncementList(query);
			list.stream().forEach(p -> {
				p.setTypeText(p.getType().getText());
			});
		}
		return new PageBean<>(list, total);
	}

}
